package com.logistics.utils.exception;

import com.logistics.utils.response.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@Component
@ControllerAdvice
@Slf4j
public class ControllerException {

    /**
     * 系统错误
     * @param ex
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<Object> errorHandler(Exception ex) {
        ex.printStackTrace();
        log.error(ex.getMessage());
        return ResponseUtil.error();
    }

    /**
     * 后台验证消息
     * @param e
     * @return
     */
    @ExceptionHandler(value = MessageException.class)
    public ResponseEntity<Object>  messageException(MessageException e){
        return ResponseUtil.checkError(e.getMessage());
    }

    /**
     * sql错误
     * @param e
     * @return
     */
    @ExceptionHandler(value = BadSqlGrammarException.class)
    public ResponseEntity<Object> mySQLSyntaxErrorException(BadSqlGrammarException e){
        e.printStackTrace();
        log.error(e.getMessage());
        return ResponseUtil.error();
    }

    /**
     * token超时
     * @param e
     * @return
     */
    @ExceptionHandler(value = JwtTimeoutException.class)
    public ResponseEntity<Object> jwtTimeException(JwtTimeoutException e){
        return ResponseUtil.authorityError();
    }

    /**
     * 续签token
     * @param e
     * @return
     */
    @ExceptionHandler(value = RenewalException.class)
    public ResponseEntity<Object> renewalException(RenewalException e){
        return ResponseUtil.renewal(e.getMessage());
    }

}
